package com.student.dao.impl;
//@Classname countryDaoImpl
//@time 15:56
//OMEN

import com.student.dao.CountryDao;
import com.student.entity.AdminInfo;
import com.student.entity.Country;
import com.student.util.DBUtil;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CountryDaoImpl implements CountryDao {
    @Override
    public int insert(Country entity) throws Exception {
        String sql = "insert  into country_info(country_code,country_name_cn,country_name_en,country_name,country_uri)values (?,?,?,?,?)";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);
        ) {
            pstm.setObject(1, entity.getCountryCode());
            pstm.setObject(2, entity.getCountryNameEn());
            pstm.setObject(3, entity.getCountryNameEn());
            pstm.setObject(4, entity.getCountryName());
            pstm.setObject(5, entity.getCountryUrl());
            return pstm.executeUpdate();

        }

    }

    @Override
    public int updateById(Country entity) throws Exception {
        String sql = "update country_info set country_name_cn=?,country_name_en=?,country_name=?,country_uri=? where country_code=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, entity.getCountryNameCn());
            pstm.setObject(2, entity.getCountryNameEn());
            pstm.setObject(3, entity.getCountryName());
            pstm.setObject(4, entity.getCountryUrl());
            pstm.setObject(5, entity.getCountryCode());
            return pstm.executeUpdate();

        }
    }

    @Override
    public int deleteById(Serializable id) throws Exception {
        String sql = "delete from country_info where country_code=?";
        try (
                Connection con = DBUtil.getConnection();
                PreparedStatement pstm = con.prepareStatement(sql);

        ) {
            pstm.setObject(1, id);
            return pstm.executeUpdate();
        }
    }

    @Override
    public Country selectById(Serializable id) throws SQLException {
        String sql = "select * from country_info where country_code=?";
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = DBUtil.getConnection();
            pstm = con.prepareStatement(sql);
            pstm.setObject(1, id);
            rs = pstm.executeQuery();
            if (rs.next()) {
                Country cn=new Country();
                cn.setCountryCode(rs.getString("country_code"));
                cn.setCountryNameCn(rs.getString("country_name_cn"));
                cn.setCountryNameEn(rs.getString("country_name_en"));
                cn.setCountryName(rs.getString("country_name"));
                cn.setCountryUrl(rs.getString("country_uri"));


                return cn;

            } else {
                throw new SQLException("没有数据");
            }


        } finally {
            DBUtil.close(con, pstm, rs);

        }
    }

    @Override
    public List<Country> select(int index, int len) throws SQLException {
        String sql ="select * from country_info order by country_code limit ?,?";
        Connection con=null;
        PreparedStatement pstm=null;
        ResultSet rs=null;
        try {
            con=DBUtil.getConnection();
            pstm=con.prepareStatement(sql);
            pstm.setObject(1,index);
            pstm.setObject(2,len);
            rs=pstm.executeQuery();
            List<Country> list=new ArrayList<>();
            while (rs.next()){
                Country cn=new Country();
                cn.setCountryCode(rs.getString("country_code"));
                cn.setCountryNameCn(rs.getString("country_name_cn"));
                cn.setCountryNameEn(rs.getString("country_name_en"));
                cn.setCountryName(rs.getString("country_name"));
                cn.setCountryUrl(rs.getString("country_uri"));
                list.add(cn);
            }
            return list;

        }finally {
            DBUtil.close(con,pstm,rs);
        }
    }

    @Override
    public long count() throws SQLException {
        return 0;
    }
}
